#include <iostream>
#include <vector>
#include <climits>

using namespace std;

// 1658. 将 x 减到 0 的最小操作数
// https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/description/

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size();
        int left = 0;
        int curSum = 0;
        int ans = INT_MAX;
        for (int i = 0; i < n + n; i++)
        {
            int curNum = nums[i % n];
            curSum += curNum;
            while(curSum > x){
                curSum -= nums[(left++)%n];
            }
            if(left > 0 && i < n - 1 || left > n && i < n + n - 1){
                continue;
            }
            if(curSum == x && i - left + 1 <= n){
                ans = min(ans, i - left + 1);
            }
        }
        return ans == INT_MAX ? -1 : ans;
    }
};

int main()
{
    return 0;
}